package cn.rkylin.oms.system.storage.service;

import cn.rkylin.core.exception.BusinessException;
import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.system.storage.dao.IStorDAO;
import cn.rkylin.oms.system.storage.domain.Stor;
import cn.rkylin.oms.system.storage.vo.StorVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库管理服务层
 */
@Service("storService")
public class StorServiceImpl extends ApolloService implements IStorService {
	
	/**
	 * 仓库数据访问对象
	 */
	@Autowired
	private IStorDAO istorDAO;

	/**
	 * 方法简要描述信息.
	 * <p>
	 * 描述: 根据条件获取仓库
	 * </p>
	 * <p>
	 * 备注: 详见顺序图
	 * </p>
	 *
	 */
	public PageInfo<StorVO> findByWhere(int page, int rows, StorVO storVo) throws Exception {
		PageInfo<StorVO> storVOList = findPage(page, rows, "getStorByCondition", storVo);
		return storVOList;
	}

	@Override
	public List<StorVO> findByProjectId(String projectId) throws Exception {
		Map<String, Object> params = new HashMap<>();
		params.put("projectId", projectId);
		return findForList("findStorageByProjectId", params);
	}

	@Override
	public List<StorVO> findByProjectIdExt(String projectId) throws Exception {
		Map<String, Object> params = new HashMap<>();
		params.put("projectId", projectId);
		return findForList("findStorageByProjectIdExt", params);
	}

	@Override
	public List<StorVO> findByProjectIdLocal(String projectId) throws Exception {
		Map<String, Object> params = new HashMap<>();
		params.put("projectId", projectId);
		return findForList("findStorageByProjectIdLocal", params);
	}
	
	@Override
	public List<StorVO> findByProjectIdCainiao(String projectId) throws Exception {
		Map<String, Object> params = new HashMap<>();
		params.put("projectId", projectId);
		return findForList("findStorageByProjectIdCainiao", params);
	}

	@Override
	public List<StorVO> findByShopId(String shopId) throws Exception {
	    Map<String, Object> params = new HashMap<>();
        params.put("shopId", shopId);
        return findForList("findStorageByShopId", params);
	}
	@Override
	public List<StorVO> findByShopIdNoDelete(String shopId) throws Exception {
		Map<String, Object> params = new HashMap<>();
		params.put("shopId", shopId);
		return findForList("findStorageByShopIdNoDelete", params);
	}

	@Override
	public StorVO getDefaultStor(String shopId) throws Exception {
		return istorDAO.getDefaultStor(shopId);
	}

	/**
	 * 方法简要描述信息.
	 * <p>
	 * 描述: 删除仓库
	 * </p>
	 * <p>
	 * 备注: 详见顺序图
	 * </p>
	 *
	 */
	@Transactional
	public void delete(String storId) throws Exception {
		try {
			List<String> listid = Arrays.asList(storId.split(","));
			for (int i = 0; i < listid.size(); i++) {
				istorDAO.delete("deleteStor", listid.get(i));
			}
		} catch (BusinessException ex) {
			ex.printStackTrace();
			throw new Exception("仓库已被使用,不能删除！");
		}
	}

	/**
	 * 方法简要描述信息.
	 * <p>
	 * 描述:获取仓库
	 * </p>
	 * <p>
	 * 备注: 详见顺序图
	 * </p>
	 *
	 */
	@Override
	public List<StorVO> getStorByCondition(StorVO storVO) throws Exception {
		try {
			return istorDAO.getStorByCondition(storVO);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
	}

	/**
	 * 方法简要描述信息.
	 * <p>
	 * 描述: 添加仓库
	 * </p>
	 * <p>
	 * 备注: 详见顺序图
	 * </p>
	 *
	 */
	@Override
	public void insert(StorVO storVO) throws Exception {
		istorDAO.insert(storVO);
	}

	/**
	 * 方法简要描述信息.
	 * <p>
	 * 描述: 获取选中仓库信息
	 * </p>
	 * <p>
	 * 备注: 详见顺序图
	 * </p>
	 *
	 */
	@Override
	public Stor selectStorDetail(String storId) throws Exception {
		Stor storModel = istorDAO.findByid(storId);
		return storModel;
	}
	
	/**
	 * 方法简要描述信息.
	 * <p>
	 * 描述: 修改仓库
	 * </p>
	 * <p>
	 * 备注: 详见顺序图
	 * </p>
	 *
	 */
	@Override
	public void update(StorVO storVO) throws Exception {
		istorDAO.update(storVO);
	}
}
